//
//  NewsDetailViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/6.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "NWNewsRequest.h"
#import "NewsListModel.h"

@interface NewsDetailViewController () <UIScrollViewDelegate,UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *newsView;

@end

@implementation NewsDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"新闻详情";
    self.view.backgroundColor = [UIColor whiteColor];
    _newsView = [[UIWebView alloc] init];
    _newsView.backgroundColor = [UIColor whiteColor];
    _newsView.scrollView.delegate = self;
    _newsView.delegate = self;
    [self.view addSubview:_newsView];
    [_newsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.equalTo(@0);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    
    [self getData];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.x > 0)
    {
        scrollView.contentOffset = CGPointMake(0, point.y);//这里不要设置为CGPointMake(0, 0)，这样我们在文章下面左右滑动的时候，就跳到文章的起始位置，不科学
    }
}

- (void)getData
{
    NWNewsRequest *request = [[NWNewsRequest alloc] init];
    [request startRequestGetNewsDetailWithDelFlag:@"0" id:_newsId];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ) {
            NewsListModel *model = [[NewsListModel alloc] initWithDictionary:result error:nil];
            [[Singleton shareInstance] startLoading];
            [_newsView loadHTMLString:model.content baseURL:nil];
        } else {
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[Singleton shareInstance] stopLoading];
}

@end
